package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.constants.AppConsrants;
import com.chengxiaoxiao.libarypartner.domain.BorrowApi;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.LoadDialogUtils;
import com.chengxiaoxiao.libarypartner.utils.LoginUtils;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.chengxiaoxiao.libarypartner.utils.UserUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.image.SmartImageView;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyBorrowActivity extends Activity
{

    private ListView listMyBorrow;

    private List<BorrowApi> listBorrowApi;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_borrow);

        listMyBorrow = (ListView) findViewById(R.id.listMyBorrow);




        if (!LoginUtils.getLoginStatus(MyBorrowActivity.this))
        {
            startActivity(new Intent(MyBorrowActivity.this, LoginActivity.class));
            return;
        }

        LoadDialogUtils.show(MyBorrowActivity.this,"","加载中...");
        initBorrow();

        listMyBorrow.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent intent = new Intent(MyBorrowActivity.this,CircleActivity.class);
                intent.putExtra(AppConsrants.BORROW_ID,listBorrowApi.get(position).getId()+"");

                startActivity(intent);
            }
        });


    }

    /**
     * 加载借阅信息
     */
    private void initBorrow()
    {
        AsyncHttpClient client = new AsyncHttpClient();

        RequestParams params = new RequestParams();
        params.add("userId", UserUtils.getNumber(MyBorrowActivity.this));

        client.post(ApiAddress.getApiDomain() + "/client/ClientServlet?op=getBorrow", params, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {
                LoadDialogUtils.hidden();

                String body = new String(responseBody);

                listBorrowApi = new ArrayList<BorrowApi>();
                try
                {
                    JSONArray jsonArray = new JSONArray(body);


                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);


                        int id = jsonObj.getInt("id");
                        int bookId = jsonObj.getInt("bookId");
                        int userId = jsonObj.getInt("userId");
                        int isDeory = jsonObj.getInt("isDeory");
                        int isReturn = jsonObj.getInt("isReturn");
                        String expireTime = jsonObj.getString("expireTime");
                        String subTime = jsonObj.getString("subTime");
                        String imageSrc = jsonObj.getString("imageSrc");
                        String bookName = jsonObj.getString("bookName");


                        BorrowApi api = new BorrowApi(id, subTime, userId, bookId, expireTime, isDeory, isReturn, bookName, imageSrc,"","");
                        listBorrowApi.add(api);
                    }

                    listMyBorrow.setAdapter(new MyBorrowAdapter());
                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                LoadDialogUtils.hidden();
                ToastUtils.makeText(getApplication(), "网络连接错误");
            }
        });


    }

    class MyBorrowAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return listBorrowApi.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listBorrowApi.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view;
            if (convertView == null)
            {
                view = View.inflate(MyBorrowActivity.this, R.layout.list_item_myborrow, null);
            } else
            {
                view = convertView;
            }

            SmartImageView imageIcon = (SmartImageView) view.findViewById(R.id.ivIcon);
            TextView textBookName = (TextView) view.findViewById(R.id.textBookName);
            TextView textSubTime = (TextView) view.findViewById(R.id.textSubTime);

            BorrowApi item = listBorrowApi.get(position);
            imageIcon.setImageUrl(item.getImageSrc());
            textBookName.setText(item.getBookName());
            textSubTime.setText(item.getSubTime());




            return view;
        }
    }


    public void back(View v)
    {
        finish();
    }
}
